﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Threading;
using System.Windows.Threading;
using HelloWorldDemo.Entity;
using System.Windows.Interop;
using System.Windows.Browser;

namespace HelloWorldDemo
{
    public partial class MainPage : UserControl
    {
        IDictionary<string, string> paras = HtmlPage.Document.QueryString;

        AutoResetEvent handle = new AutoResetEvent(true);
        User u = null;
        public MainPage()
        {
            InitializeComponent();
            Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_FullScreenChanged);
        }

        void LayoutRoot_KeyDown(object sender, KeyEventArgs e)
        {
           // this.lblName.Text = e.Key.ToString();
            this.lblName.Text = paras["name"];
            this.lblAddress.Text = paras["addr"];
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //user
            User u = GetUser();
            this.lblName.DataContext = u;
            this.lblAddress.DataContext = u;
            //user list
            List<User> lst = Users();
            this.dgGrid.ItemsSource = lst;
            //dynamic load user-control
        }

        private User GetUser()
        {
            u = new User();
            u.Name = "爱因斯坦";
            u.Address = "美国 洛杉矶 XX图书馆";
            return u;
        }

        private List<User> Users()
        {
            List<User> lst = new List<User>();
            for (int i = 0; i < 15; i++)
            {
                User u = new User();
                u.Name = "Name" + i.ToString();
                u.Address = "Address" + i.ToString();
                u.Age = i;
                u.IsActive = i % 2 == 0;
                u.Birthday = DateTime.Today.AddYears(-i);
                lst.Add(u);
            }
            return lst;
        }

        private void tbDemo_MouseEnter(object sender, MouseEventArgs e)
        {
            //this.tbDemo.Text = "I get here";
            //double opc = 1.0;
            //try
            //{
            //    DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
            //    timer.Tick += (s, evt) =>{
            //        this.tbDemo.Text = DateTime.Now.ToString();
            //        int r = 0;
            //        int g = 0;
            //        int b = 0;
            //        r = new Random().Next(99);
            //        g = new Random().Next(99);
            //        b = new Random().Next(99);
            //        opc = new Random().NextDouble();
            //        this.LayoutRoot.Background.Opacity = opc;
            // };
            //    timer.Interval = TimeSpan.FromMilliseconds(1000);
            //    timer.Start();

                //while (true)
                //{
                //    handle.WaitOne(10000);
                //    new System.Threading.Thread(() =>
                //    {                       
                //        Deployment.Current.Dispatcher.BeginInvoke(() =>
                //        {
                //            int r = 0;
                //            int g = 0;
                //            int b = 0;
                //            r = new Random().Next(99);
                //            g = new Random().Next(99);
                //            b = new Random().Next(99);
                //            opc = new Random().NextDouble();
                //            this.LayoutRoot.Background.Opacity = opc;
                //            this.LayoutRoot.Background = new SolidColorBrush(Color.FromArgb(255, (byte)r, (byte)b, (byte)g));
                //        });
                //    }).Start();
                //}

            //}
            //catch(Exception err)
            //{
            //    this.tbDemo.Text = err.Message;
            //}
        }

        //private Timer DispatcherTimer()
        //{
        //    throw new NotImplementedException();
        //}
   
        private void tbDemo_MouseLeave(object sender, MouseEventArgs e)
        {
            //this.tbDemo.Text = "Hello World";
        }

        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            u.Name = "Aiystan";
            u.Address = "US Losanles XX Lib";
        }

        private void btnToggleFull_Click(object sender, RoutedEventArgs e)
        {
            Content obj = Application.Current.Host.Content;           
            obj.IsFullScreen = !obj.IsFullScreen;
        }

        void Content_FullScreenChanged(object sender, EventArgs e)
        {
            if (Application.Current.Host.Content.IsFullScreen)
            {
                this.btnToggleFull.Content = "窗口";
                this.btnToggleFull.Background = new SolidColorBrush(Colors.Orange);
            }
            else
            {
                this.btnToggleFull.Content = "全屏";
                this.btnToggleFull.Background = new SolidColorBrush(Colors.LightGray);
            }
        }

        private void btnDrag_Click(object sender, RoutedEventArgs e)
        {
            //DragDrop dd = new DragDrop();
            ImgWave dd = new ImgWave();
            dd.Show();
        }
    }
}
